"use strict";

/**
 * author:Administrator
 * date:2015/11/30
 * desc: user face dao
 */
var co = require('co');
var Model = require('../model/face_star.js');
var daoStar = require('../dao/face_star.js');
var daoPart = require('../dao/face_part.js');
var daoUface = require('../dao/face_user.js');
var daoRendered = require('../dao/face_rendered.js');
var daoCounter = require('../dao/counter.js');
var mongoose = require('../conn/mongoose.js');

// 清理数据
// not work

function * removeOldData() {
  console.log('removeOldData');
  yield daoStar.cleanCollection();
  yield daoPart.cleanCollection();
  yield daoUface.cleanCollection();
  yield daoRendered.cleanCollection();
  // set the couter to be right
  var types = daoCounter.TypesEnum;
  yield daoCounter.setCounter(types.faceStar, 100);
  yield daoCounter.setCounter(types.facePart, 0);
  yield daoCounter.setCounter(types.faceUser, 0);
  yield daoCounter.setCounter(types.faceRendered, 0);
}


var list = [];
for (var i = 1; i <= 12; i++) list.push(i);

// 准备 明星头像
function * prepareFaceStar() {
  console.log('prepareFaceStar');
  var starList = list.map(function (i) {
    return {
      name: 'star_' + i,
      path: '/data/face/face_list/' + i + '.jpg',
      rpath: '/face_list/%i.jpg'.replace('%i', i + ''),
      starid: i
    }
  });
  for (var item of starList) {
    console.log('insert ', item);
    yield daoStar.save(item);
  }
}

// 准备 明星组件
function * prepareFaceParts() {
  console.log('prepareFaceParts');
  var partCounter = daoCounter.bindCounter(daoCounter.TypesEnum.facePart);
  var partList = [];
  for (var i of list) {
    var imgArr = ['default_brow', 'default_eye', 'default_nose', 'default_mouse'];
    var descArr = ['眉毛', '眼睛', '鼻子', '嘴巴'];
    for (var m = 0; m < 4; m++) {
      var partid = yield partCounter();
      var rurl = '/face_part/' + imgArr[m] + '.jpg';
      var desc = '明星starid=' + i + ' 的' + descArr[m];
      partList.push({
        partid: partid,
        desc: desc,
        otype: m,
        starid: i,
        rurl: rurl
      });
    }
  }

  for (var item of partList) {
    console.log('insert ', item);
    yield daoPart.save(item);
  }
}

co(function *() {
  yield removeOldData();
  yield prepareFaceStar();
  yield prepareFaceParts();
  mongoose.connection.close(); // 关闭db链接
  console.log('all done');
});
